let storage = bui.storage();
let org;
let language;
let translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            org = storage.get("org", 0)
            language = storage.get("language", 0)
            translate = global.translate
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }

            let uiLoading = bui.loading({
                appendTo:"#typeLoading",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"加载中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });
            // uiLoading.show()

            router.$("#typeFh").click(function () {
                storage.set("tzPage", 0)
                router.replace({url: "pages/mainIndex/mainIndex.html"});
            })

            let data = storage.get("termList", 0)
            // bui.ajax({
            //     url: global.api.getCountryList,
            //     data: {type: '1', parent: '1'},
            //     method: "POST"
            // }).then(function(res){
            //     let data = res.data
                if (data) {
                    let fontSize = '15px'
                    if (language !== "chinese_simplified") {
                        fontSize = '0.18rem'
                    }
                    let html = ""
                    data.forEach((da, index) => {
                        if (index == 0) {
                            html += '<div class="tab" name="'+index+'" id="'+da.id+'" mc="'+da.name+'" style="border-left: red 2px solid; background-color: white">\n'+
                                '           <p id="tabLabel'+index+'" class="cityText" style="color: red; font-size: '+fontSize+'">'+da.name+'</p>\n'
                            getTypeChildren(da.id, da.name,global.api.getCountryList)
                        } else {
                            html += '<div class="tab" name="'+index+'" id="'+da.id+'" mc="'+da.name+'">\n'+
                                '           <p id="tabLabel'+index+'" class="cityText" style="font-size: '+fontSize+'">'+da.name+'</p>\n'
                        }
                        html += '     </div>'
                    })
                    router.$("#list").html(html)
                    try {
                        translate.execute();
                    }catch (e) {
                        console.log("切换语言报错")
                    }

                    router.$(".tab").click(function () {
                        uiLoading.show()
                        let id = router.$(this).attr("id")
                        let xh = router.$(this).attr("name")
                        let mc = router.$(this).attr("mc")

                        if(router.$("#tabLabel" + xh).css('color') != 'red') {
                            router.$(".tab").css({
                                'border-left': 0,
                                'background-color': 'white'
                            });
                            router.$(".cityText").css({
                                'color': 'black'
                            });

                            router.$(this).css({
                                'border-left': 'red 2px solid',
                                'background-color': 'white'
                            })
                            router.$("#tabLabel" + xh).css({
                                'color': 'red'
                            })

                            getTypeChildren(id, mc,global.api.getCountryList, uiLoading)
                        }
                    })
                }
                uiLoading.stop()
            // })
        }
    };

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})

function getTypeChildren(id, mc,url, uiLoading) {
    let data = storage.get("term" + id, 0)
    // bui.ajax({
    //     url: url,
    //     data: {parent: parseInt(id)},
    //     method: "POST"
    // }).then(function(res){
    //     let data = res.data
        let html = ""
        if (data) {
            let fontSize = '15px'
            if (language !== "chinese_simplified") {
                fontSize = '0.2rem'
            }
            let xb = 0;
            data.forEach((r,index) => {
                if (xb % 3 == 0) {
                    html += '</div><div class="bui-box" style="height: 100%">'
                }
                xb++

                html += '<div class="span1 countryImg" id="'+r.id+'" name="'+r.name+'">'+
                    '<p class="cityText" style="font-size: '+fontSize+'">'+r.name+'</p></div>'

            })
            let len = data.length
            if (len % 3 != 0) {
                for (let i = 0; i < 3- (len%3); i++) {
                    html += '<div class="span1"></div>'
                }
            }
            html += "</div>"
        }
        router.$("#type").html(html)
        router.$(".countryImg").click(function () {
            let cate = $(this).attr("id")
            let cateStr = $(this).attr("name")
            storage.set('menCate', cate + '-' + cateStr + '-' + org.id)
            // router.load({ url: "pages/flPage/flPage.html",param:{tzSign: '2'}});
            router.load({ url: "pages/flPage/flPage.html"});
        })

        if (uiLoading) {
            uiLoading.stop()
        }

    try {
        translate.execute();
    }catch (e) {
        console.log("切换语言报错")
    }
    // })
}
